/*
 * Copyright (c) 2015, the Dart project authors.
 *
 * Licensed under the Eclipse Public License v1.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package vm.element

// This file is generated by the script: pkg/vm_service/tool/generate.dart in dart-lang/sdk.

import com.google.gson.JsonArray
import com.google.gson.JsonObject

/**
 * 请参阅 getCpuSamples 和 CpuSample。
 */
class CpuSamples(json: JsonObject) : Response(json) {

    /**
     * 在相关样本中看到的函数列表。可以使用 `CpuSample` `stack` 中提供的索引查找这些引用，以确定堆栈上是哪个函数。
     */
    fun getFunctions(): ElementList<ProfileFunction> {
        return object : ElementList<ProfileFunction>(json.get("functions").asJsonArray) {
            override fun basicGet(array: JsonArray, index: Int): ProfileFunction {
                return ProfileFunction(array.get(index).asJsonObject)
            }
        }
    }

    /**
     * 样本的最大可能堆栈深度。
     */
    fun getMaxStackDepth(): Int {
        return getAsInt("maxStackDepth")
    }

    /**
     * VM 的进程 ID。
     */
    fun getPid(): Int {
        return getAsInt("pid")
    }

    /**
     * 返回的样本数。
     */
    fun getSampleCount(): Int {
        return getAsInt("sampleCount")
    }

    /**
     * 分析器的采样率（以微秒为单位）。
     */
    fun getSamplePeriod(): Int {
        return getAsInt("samplePeriod")
    }

    /**
     * 在 `[timeOriginMicros, timeOriginMicros + timeExtentMicros]` 范围内收集的样本列表
     */
    fun getSamples(): ElementList<CpuSample> {
        return object : ElementList<CpuSample>(json.get("samples").asJsonArray) {
            override fun basicGet(array: JsonArray, index: Int): CpuSample {
                return CpuSample(array.get(index).asJsonObject)
            }
        }
    }

    /**
     * 返回的样本所覆盖的时间段。
     */
    fun getTimeExtentMicros(): Int {
        return getAsInt("timeExtentMicros")
    }

    /**
     * 返回的样本被收集的时间段的开始时间。
     */
    fun getTimeOriginMicros(): Int {
        return getAsInt("timeOriginMicros")
    }
}
